import axios from 'axios'
import qs from 'qs';
import Vue from 'vue'
const instance = axios.create({
        baseURL: 'http://localhost:3000',
    })
    // axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.transformRequest = data => qs.stringify(data);
instance.interceptors.request.use(function(config) {

    // Do something before request is sent
    if (localStorage.token) {
        config.headers.Authorization = 'Bearer ' + localStorage.token
    }
    // if (config.method === 'post') {
    //     console.log(config);
    //     config.data = qs.stringify(config.data);

    // }
    return config;
}, function(error) {
    Vue.prototype.$message.error({
            type: 'error',
            message: error
        })
        // Do something with request error
    return Promise.reject(error);
});
instance.interceptors.response.use(function(config) {
    // Do something before request is sent
    if (localStorage.token) {
        config.headers.Authorization = 'Bearer ' + localStorage.token
    }
    return config;
}, function(error) {
    if (error.response.data) {
        Vue.prototype.$message.error({
            type: 'error',
            message: error.response.data
        })
    }
    // Do something with request error
    return Promise.reject(error);
});


export default instance;